<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Client extends CI_Controller {

        function __construct()
        {
                parent::__construct();
                $this->load->database();
                $this->load->helper('url');
        }


        /*
        function index($page=1)
        {//get category
                $this->load->model('client_model');
                $category=$this->client_model->get_category();
                if($category!=FALSE)
                {
                        $index['category']=$category;
                        $book_nums=$this->get_books_num();
                        $index['b_num']=$book_nums;
                        $books_list=$this->show_books($page);
                        $index['b_list']=$books_list;
                        echo json_encode($index);
                }
        }

        function get_books_num()
        {
                $this->load->model('client_model');
                $bnum=$this->client_model->get_books_num();
                return $bnum;
        }
        function show_books($page=1)
        {
                $this->load->model('client_model');
                $category=$this->client_model->show_books($page);
                return $category;
        }
        */

        function index($fid=0,$type=0, $page=1) {
                $this->load->model('client_model');
                $pic=$this->client_model->get_category_pic($fid,$type);
                $categorys = $this->client_model->get_second_category($fid);
                foreach ($categorys as &$cate) {
                        $cate['b_num']=$this->every_category_nums($cate['cate_id']);
                        $result=$this->every_category_list($cate['cate_id'],$page);
                        log_message("debug", "category id:".$cate['cate_id']." book num:".$cate['b_num']
                                    ." books:".json_encode($result));
                        if ($result != false) {
                                $cate['books'] = $result;
                        }
                }

                $fcate=array("pic"=>$pic,"sub_category"=>$categorys);
                echo json_encode($fcate);
        }

        function all_category($page=1) {
                $this->load->model('client_model');
                $s_cate=array();
                #the array represent the first category
                $all_fid=array(1,2,3,4,5);
                foreach ( $all_fid as $fid) {
                        $categorys = $this->client_model->get_second_category($fid);
                        foreach ($categorys as $key => &$cate) {
                                # yes, this is a piece of shit, we need the "最新推荐" category to be
                                # displayed on the index api, but must be ignored here, so this wierd
                                # code appears
                                if ($cate['bc']== "最新推荐") {
					unset($categorys[$key]);
                                        continue;
                                }
                                $cate['b_num']=$this->every_category_nums($cate['cate_id']);
                                $result=$this->every_category_list($cate['cate_id'],$page);
                                log_message("debug", "category id:".$cate['cate_id']." book num:".$cate['b_num']
                                            ." books:".json_encode($result));
                                if ($result != false) {
                                        $cate['books'] = $result;
                                }
                        }
                        $s_cate=array_merge($s_cate, $categorys);
                }
                echo json_encode($s_cate);

        }

        function all_popular_category($page=1) {
                $this->load->model('client_model');
                $s_cate=array();

                # the array represent the root category, we hacked the get_second_category() function
                # so we can use it to get first category, just for easiness of the client
                $all_fid=array(0);
                foreach ( $all_fid as $fid) {
                        $categorys = $this->client_model->get_second_category($fid);
                        foreach ($categorys as &$cate) {
                                $cate['b_num']=$this->client_model->every_popular_category_nums($cate['cate_id']);
                                $result=$this->client_model->every_popular_category_list($cate['cate_id'],$page);
                                log_message("debug", "category id:".$cate['cate_id']." book num:".$cate['b_num']
                                            ." books:".json_encode($result));
                                if ($result != false) {
                                        $cate['books'] = $result;
                                }
                        }
                        $s_cate=array_merge($s_cate, $categorys);
                }
                echo json_encode($s_cate);
        }

        function every_category($cate_id,$page=1)
        {
                $category['b_num']=$this->every_category_nums($cate_id);
                $result=$this->every_category_list($cate_id,$page);
                if($result==false)
                {
                        echo json_encode($category);
                }
                else
                {
                        $category['b_list']=$result;
                        echo json_encode($category);
                }
        }
        function every_category_nums($cate_id)
        {//get total records
                $this->load->model('client_model');
                $nums=$this->client_model->every_category_nums($cate_id);
                return $nums;
        }
        function every_category_list($cate_id,$page)
        {//get every category page list message
                $this->load->model('client_model');
                $list=$this->client_model->every_category_list($cate_id,$page);
                return $list;
        }


        function get_book_message($prod_id)
        {
                $this->load->model('client_model');
                $message=$this->client_model->get_book_message($prod_id);
                echo json_encode($message);
        }


        function purchase()
        {
                $this->load->model('client_model');
                $result=$this->client_model->purchase();
                echo json_encode($result);
        }


        function search()
        {
                $this->load->model('client_model');
                $result=$this->client_model->search();
                echo json_encode($result);
        }

        function get_comments($pid,$page=1)
        {
                $cnum=$this->get_comments_nums($pid);
                if($cnum>0)
                {
                        $comment['cnum']=$cnum;
                        $comment['co_list']=$this->get_comments_list($pid,$page);
                        echo json_encode($comment);
                }
        }
        function get_comments_nums($pid)
        {
                $this->load->model('client_model');
                $result=$this->client_model->get_comments_nums($pid);

             return $result;
        }
        function get_comments_list($pid,$page)
        {
                $this->load->model('client_model');
                $result=$this->client_model->get_comments_list($pid,$page);
                return $result;
        }


        function insert_comments()
        {
                $this->load->model('client_model');
                $result=$this->client_model->insert_comments();
                echo json_encode($result);
        }
}




